<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2013
 * ---
 * Repraesentiert einen User
 *
 * @author Robin
 */
class User {
    private $id = null;
    private $login = null;
    private $password = null;
    private $nicename = null;
    private $email = null;
    private $registered = null;
    private $active = null;
    private $role = null;
    private $right = null;

    public function __construct($data = array()) {
        if (isset($data['user_id'])) $this->id = (int) $data['user_id'];
        if (isset($data['user_login'])) $this->login = $data['user_login'];
        if (isset($data['user_password'])) $this->password = $data['user_password'];
        if (isset($data['user_nicename'])) $this->nicename = $data['user_nicename'];
        if (isset($data['user_email'])) $this->email = $data['user_email'];
        if (isset($data['user_registered'])) $this->registered = $data['user_registered'];
        if (isset($data['user_active'])) $this->active = (bool) $data['user_active'];
        if (isset($data['user_role'])) $this->role = (int) $data['user_role'];
        if (isset($data['role_right'])) $this->right = (int) $data['role_right'];
    }
    
    /**
     * 
     * @param type $id
     * @return \User
     */
    public static function getUserById($id) {
        $db = new Database();

        $sql = "SELECT u.*, r.role_right FROM ff_users u, ff_roles r WHERE u.user_role = r.role_id AND u.user_id = :id";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $id, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new User($row);
    }
    
    /**
     * 
     * @param type $login
     * @return \User
     */
    public static function getUserByLogin($login) {
        $db = new Database();

        $sql = "SELECT u.*, r.role_right FROM ff_users u, ff_roles r WHERE u.user_role = r.role_id AND u.user_login = :login";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":login", $login, PDO::PARAM_STR);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new User($row);
    }
    
    /**
     * 
     * @param type $username
     * @param type $password
     * @return boolean
     */
    public static function checkLogin($username, $password) {
        $db = new Database();
        
        $sql = "SELECT COUNT(*) AS anzahl FROM ff_users u, ff_roles r WHERE u.user_role = r.role_id AND u.user_login = :username AND u.user_password = :password AND r.role_right = :right";
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":username", $username, PDO::PARAM_STR);
        $stmt->bindValue(":password", md5($password), PDO::PARAM_STR);
        $stmt->bindValue(":right", 1, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;
        
        if ($row["anzahl"] == "1") {
            return true;
        }
        else {
            return false;
        }
    }
    
    public function getRight() {
        return $this->right;
    }
    
    public function getId() {
        return $this->id;
    }

    public function getLogin() {
        return $this->login;
    }

    public function getPassword() {
        return $this->password;
    }

    public function getNicename() {
        return $this->nicename;
    }

    public function getEmail() {
        return $this->email;
    }

    public function getRegistered() {
        return $this->registered;
    }

    public function getActive() {
        return $this->active;
    }

    public function getRole() {
        return $this->role;
    }
}

?>
